package com.lt.report.service.doubleService.impl;

import com.lt.report.bean.doubleBean.CityConfirmInfo;
import com.lt.report.bean.doubleBean.TdMStaff;
import com.lt.report.controller.common.TableResponseVO;
import com.lt.report.dao.doubleDao.CityConfirmInfoMapper;
import com.lt.report.dao.doubleDao.TdMStaffMapper;
import com.lt.report.service.doubleService.DoubleService;
import com.lt.report.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DoubleServiceImpl implements DoubleService {
    @Autowired
    TdMStaffMapper tdMStaffMapper;

    @Autowired
    CityConfirmInfoMapper cityConfirmInfoMapper;


    @Override
    public TdMStaff queryRole(Map<String, Object> map) throws Exception {
        TdMStaff tdMStaff = tdMStaffMapper.queryStaffById(map);
        if (tdMStaff==null){
            tdMStaff = new TdMStaff();
            tdMStaff.setRole("系统中没有该用户的记录，请确认登录人员工编号。");
            return tdMStaff;
        }
        Object departCode = tdMStaff.getDepartCode();
        if(departCode.equals("")){
            tdMStaff = new TdMStaff();
            tdMStaff.setRole("该用户没有部门，请确认登录人员工编号。");
            return tdMStaff;
        }

        if (departCode.toString().equals("NMCUA001")){
            tdMStaff.setRole("administrator");
        }else {
            tdMStaff.setRole("normal");
        }
        return tdMStaff;
    }

    /**
     * 获取登陆人信息
     *
     * @param map
     * @return
     * @throws Exception
     */
    @Override
    public TdMStaff queryRoleList(Map<String, Object> map) throws Exception {
        log.info("param:", map.toString());
        TdMStaff tdMStaff = tdMStaffMapper.queryStaffById(map);
        log.info("account is :", tdMStaff.getDepartCode());
        return tdMStaff;
    }

    /*
    将结果转换成bootstraptable格式的对象
     */
    @Override
    public TableResponseVO queryBySearchMap(Map<String,Object> map) throws Exception {
        List<CityConfirmInfo> list = cityConfirmInfoMapper.queryBySearchMap(map);
        Util<CityConfirmInfo> util = new Util<>();
        Long count = cityConfirmInfoMapper.countBySearchMap(map);
        TableResponseVO tableResponseVO = util.turnTOBootStrapTable(list,count);
        return tableResponseVO;
    }

    @Override
    public int insert(CityConfirmInfo record) throws Exception {
        Date time = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(time);
        record.setCollectionTime(date);
        return cityConfirmInfoMapper.insert(record);
    }

    @Override
    public CityConfirmInfo selectByPrimaryKey(String id) {
        return cityConfirmInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(CityConfirmInfo record) throws Exception {
        Date time = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(time);
        record.setCollectionTime(date);
        return cityConfirmInfoMapper.updateByPrimaryKeySelective(record);
    }

    //确认接口
    @Override
    public int updateConfirm(CityConfirmInfo record) {
        Date time = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(time);
        record.setConfirmTime(date);
        return cityConfirmInfoMapper.updateConfirm(record);
    }

    @Override
    public int deleteByPrimaryKey(String id) throws Exception {
        return cityConfirmInfoMapper.deleteByPrimaryKey(id);
    }




}
